﻿namespace PMS.Client.FrmImportAndExport
{
    using AsyncFactory;
    using PMS.BLL;
    using PMS.Common;
    using PMS.Model;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Linq;
    using System.Windows.Forms;

    public class FrmOrganDataExport : Form
    {
        private Button btnCancel;
        private Button btnGetUserData;
        private Button btnSave;
        private IContainer components = null;
        private Label label2;
        private string rootOrganName = "";
        private string saveFilePath = "";
        private TextBox UserData;

        public FrmOrganDataExport()
        {
            this.InitializeComponent();
            this.rootOrganName = PMS.Common.Keys.AuthorityOrganList.FirstOrDefault<AuthorityOrgan>().OrganName;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnGetUserData_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog dialog = new SaveFileDialog())
            {
                dialog.Filter = "机构备份|*.xml";
                dialog.FileName = this.rootOrganName + "_数据备份_" + DateTime.Now.ToString("yyyyMMdd") + ".xml";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    this.saveFilePath = dialog.FileName;
                    this.UserData.Text = this.saveFilePath;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.saveFilePath != "")
            {
                bool isSuccess = false;
                AsyncProcess.Process(delegate {
                    try
                    {
                        OrganExportAndImport import = new OrganExportAndImport {
                            OrganList = new OrganBLL().GetList(),
                            OrganChangeLogList = new OrganChangeLogBLL().GetList(),
                            UserList = new UserBLL().GetList(0, null, ""),
                            UserChangeLogList = new UserChangeLogBLL().GetList()
                        };
                        SerializationHelper.Save(import, this.saveFilePath);
                        isSuccess = true;
                    }
                    catch (Exception exception)
                    {
                        Log.Write(exception.Message);
                        isSuccess = false;
                    }
                }, delegate {
                    if (isSuccess)
                    {
                        MessageBox.Show("导出成功！");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("导出失败！");
                    }
                }, true, LoadingImageType.Strip);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FrmOrganDataExport));
            this.btnCancel = new Button();
            this.btnSave = new Button();
            this.UserData = new TextBox();
            this.label2 = new Label();
            this.btnGetUserData = new Button();
            base.SuspendLayout();
            this.btnCancel.BackColor = Color.White;
            this.btnCancel.FlatStyle = FlatStyle.Popup;
            this.btnCancel.Location = new Point(0x124, 0x63);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x4b, 0x17);
            this.btnCancel.TabIndex = 0x3f;
            this.btnCancel.Text = "取消";
            this.btnCancel.UseVisualStyleBackColor = false;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnSave.BackColor = Color.White;
            this.btnSave.FlatStyle = FlatStyle.Popup;
            this.btnSave.Location = new Point(0x5f, 0x63);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new Size(0x4b, 0x17);
            this.btnSave.TabIndex = 0x3e;
            this.btnSave.Text = "导出";
            this.btnSave.UseVisualStyleBackColor = false;
            this.btnSave.Click += new EventHandler(this.btnSave_Click);
            this.UserData.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.UserData.BorderStyle = BorderStyle.FixedSingle;
            this.UserData.Location = new Point(0x5f, 0x29);
            this.UserData.Name = "UserData";
            this.UserData.ReadOnly = true;
            this.UserData.Size = new Size(0x110, 0x15);
            this.UserData.TabIndex = 0x3d;
            this.label2.AutoSize = true;
            this.label2.Location = new Point(0x30, 0x2b);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x29, 12);
            this.label2.TabIndex = 60;
            this.label2.Text = "请选择";
            this.btnGetUserData.BackColor = Color.Gainsboro;
            this.btnGetUserData.FlatAppearance.BorderColor = Color.DimGray;
            this.btnGetUserData.FlatStyle = FlatStyle.Flat;
            this.btnGetUserData.Location = new Point(0x15a, 0x29);
            this.btnGetUserData.Name = "btnGetUserData";
            this.btnGetUserData.Size = new Size(0x15, 0x15);
            this.btnGetUserData.TabIndex = 0x40;
            this.btnGetUserData.Text = "…";
            this.btnGetUserData.UseVisualStyleBackColor = false;
            this.btnGetUserData.Click += new EventHandler(this.btnGetUserData_Click);
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            base.ClientSize = new Size(0x1c1, 0x94);
            base.Controls.Add(this.btnGetUserData);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnSave);
            base.Controls.Add(this.UserData);
            base.Controls.Add(this.label2);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.Name = "FrmOrganDataExport";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "机构数据导出";
            base.ResumeLayout(false);
            base.PerformLayout();
        }
    }
}

